const sequelize = require('../db.js')
const { QueryTypes } = require('sequelize');
const router = require('koa-router')()
const jwt = require('koa-jwt');
router.prefix('/order')
router.get('/:type',async ctx=>{
	let {type} = ctx.params
	ctx.body={
		code:200,
		data:await sequelize.query(`select * from goods2 as d,(select * from orderInfos as a,(select orderInfoId from orders where type=${type}) as b where a.id=b.orderInfoId) as c where d.id=c.goodsId`,{ type: QueryTypes.SELECT })
	}
})
router.post('/add',jwt({ secret:'yan' }),async ctx=>{
	let {id} = ctx.state.user
	let {orderInfoId,type,xzsz} = ctx.request.body
	const r = await sequelize.query(`insert into orders (id,userId,type,orderInfoId) values (null,${id},${type},"${orderInfoId.toString()}")`)
	xzsz.forEach(async item=>{
		await sequelize.query(`insert into orderInfos (id,goodsId,goodsNum) values ("${orderInfoId.toString()}",${item.goodsId},${item.goodsNum})`)
		await sequelize.query(`delete from cart where id=${item.id}`)
	})
	
		ctx.body={
			code:200,
			data:'添加成功'
		}
})
module.exports = router